package common;
/*
 * 格式化数据处理封装类
 */
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
public class MyFormat {
	/*
	 * 日期
	 */
	static SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyy-MM");
	static SimpleDateFormat MMdd = new SimpleDateFormat("MM-dd");
	static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat yyMMdd = new SimpleDateFormat("yy-MM-dd");
	static SimpleDateFormat MM = new SimpleDateFormat("MM");
	static SimpleDateFormat dd = new SimpleDateFormat("dd");

	public static SimpleDateFormat getyyyyMM() {return yyyyMM;}
	public static SimpleDateFormat getMMdd() {return MMdd;}
	public static SimpleDateFormat getYyyyMMdd() {return yyyyMMdd;}
	public static SimpleDateFormat getYyMMdd() {return yyMMdd;}
	public static SimpleDateFormat getMM() {return MM;}
	public static SimpleDateFormat getDd() {return dd;}
	
	/*
	 * 数字 
	 */
	static DecimalFormat df01 = new DecimalFormat("#0.0");
	static DecimalFormat df02 = new DecimalFormat("#0.00");
	static DecimalFormat df03= new DecimalFormat("#0.000");
	static DecimalFormat df04= new DecimalFormat("#0.0000");
	
	public static DecimalFormat getDf01() {return df01;}
	public static DecimalFormat getDf02() {return df02;}
	public static DecimalFormat getDf03() {return df03;}
	public static DecimalFormat getDf04() {return df04;}

	/*
	 * 自定义
	 */
	static SimpleDateFormat yyyyMMdd_CN = new SimpleDateFormat("yyyy年M月d日");
	static SimpleDateFormat ymdHM_CN = new SimpleDateFormat("yyyy年M月d日 HH时mm分");
	static SimpleDateFormat ymdHM_CN2 = new SimpleDateFormat("yyyy年M月d日 HH:mm");
	static SimpleDateFormat yyyyMM_CN = new SimpleDateFormat("yyyy年M月");
	static SimpleDateFormat dhh24_CN = new SimpleDateFormat("d日HH时");
	
	public static SimpleDateFormat getYyyyMMdd_CN() {return yyyyMMdd_CN;}
	public static SimpleDateFormat getDhh24_CN() {return dhh24_CN;}
	public static SimpleDateFormat getymdHM_CN() {return ymdHM_CN;}
	public static SimpleDateFormat getymdHM_CN2() {return ymdHM_CN2;}
	public static SimpleDateFormat getYyyyMM_CN() {return yyyyMM_CN;}
	
	/*
	 * 字符串正则表达式
	 */
	static final String dateDt3Str = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}.[0-9]{0,10}";
	static final String dateDtStr = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}";
	static final String dateFtStr = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}";
	static final String dateYmStr = "[0-9]{4}-[0-9]{1,2}";
	static final String dateYStr = "[0-9]{4}";

	public static String getDateDt3Str() {return dateDt3Str;}
	public static String getDateDtStr() {return dateDtStr;}
	public static String getDateFtStr() {return dateFtStr;}
	public static String getDateYmStr() {return dateYmStr;}
	public static String getDateYStr() {return dateYStr;}

	/*
	 * 给我个日期类型(yyyy-MM-dd)的字符串，我返回相关内容
	 * @paras dateStr：日期字符串，格式为yyyy-MM-dd
	 * @paras relx：返回的类型，年、月、日分别为1、2、3
	 * @return 返回日期字符串中指定类型的内容
	 */
	public static String dateStrFmt(String dateStr, int relx) {
		//判断时间为 yyyy-mm-dd
		if (dateStr != null) {
			
			if(dateStr.matches(dateFtStr)){
				if (relx == 1) {
					//返回年
				} else if (relx == 2) {
					//返回月
				} else if (relx == 3) {
					//返回日，截取第二个‘-’到尾
					return Integer.parseInt(dateStr.substring(dateStr.lastIndexOf("-") + 1)) + "";
				}
			}else if(dateStr.matches(dateDtStr) || dateStr.matches(dateDt3Str)){
				if (relx == 1) {
					//返回年
				} else if (relx == 2) {
					//返回月
				} else if (relx == 3) {
					//返回日，截取第二个‘-’ 到 第一个‘空格’
					return Integer.parseInt(dateStr.substring(dateStr.lastIndexOf("-") + 1 , dateStr.indexOf(" "))) + "";
				}
			}
		}
		return "";
	}
	/*
	 * 字符串是否为日期/时间格式
	 */
	public static boolean strIsDt(String str,boolean sj){
		if(str==null){ return false;}
		if(sj){if(str.matches(dateFtStr)||str.matches(dateDtStr)||str.matches(dateDt3Str)){return true;}}//验证时间
		else{if(str.matches(dateFtStr)){return true;}}//不验证时间
		return false;
	}
	
	/*
	 * 字符编码的静态字符串
	 */
	static final String GBK = "GBK";
	static final String i8_1 = "ISO-8859-1";
	static final String u8 = "utf-5";

	public static String getGBK() {return GBK;}
	public static String getI8_1() {return i8_1;}
	public static String getU8() {return u8;}
	
}